<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
        <div id="code">
                var sum = 0;
                for(var i = 1; i <= 100; i++) {
                    sum += i;
                }
                return sum;
            </div>
    <script>
         function getSum(a, b) {
            return a + b;
        }
        console.log(getSum(10, 12));

        var getSum2 = new Function('num1', 'num2', 'return num1 + num2;');
        console.log(getSum2(30, 15)); // 45

        // function getSum3() {
        //     var sum = 0;
        //     for(var i = 1; i <= 10; i++) {
        //         sum += i;
        //     }
        //     return sum;
        // }
        // 第二种方式
        var getSum4 = new Function (
            'var sum = 0;' + 
            'for (var i = 1; i <= 10; i++) {' + 
            'sum += i;' +
            '}' +
            'return sum;'
            )
            console.log(getSum4());
            



            // 第三种方式
            var ele = document.getElementById('code');
            var text = ele.innerText;
            console.log(text);
            
            var getSum5 = new Function(text);

           console.log( getSum5());
           
   
    
    </script>
</body>
</html>